<?php
/**
 * Created by PhpStorm.
 * User: openoter
 * Date: 2018/12/9
 * Time: 23:05
 */

namespace app\admin\controller;


use app\admin\model\Cls;
use app\admin\model\Teacher;
use app\services\Oss;
use think\Config;
use think\Request;

class TeacherController extends BaseController {

    public function index() {
        $teacher = Teacher::where(['is_del'=>1])->paginate(10);
        $this->assign('teachers', $teacher);
        return view('index');
    }

    public function add(Request $request){
        if ($request->isPost()) {
            $file = $request->file('head_url');
            $filename = '';
            if ($file) {
                $filename = $this->upload($file);
            }
            /*if ($file) {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    $filename = "/uploads/".$info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $this->error($file->getError(), 'add');
                }
            }*/

            $param = $request->post();
            $result = $this->validate($param, 'app\admin\validate\Teacher');
            if (true !== $result)
                return $this->error($result, 'add');
            $param['head_url'] = $filename;
            $param['addtime'] = time();
            $res = Teacher::create($param);
            if ($res) {
                return $this->success('添加成功', 'index');
            } else {
                return $this->error('添加失败', 'add');
            }
        } else {
            $cls = Cls::all();
            $this->assign('cls', $cls);
            return view('add');
        }
    }

    public function edit(Request $request, $id){
        if ($request->isPost()) {
            $file = $request->file('head_url');
            $filename = '';
            if ($file) {
                $filename = $this->upload($file);
            }
            /*$filename = '';
            if ($file) {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    $filename = "/uploads/".$info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $this->error($file->getError(), 'add');
                }
            }*/

            $param = $request->post();
            $result = $this->validate($param, 'app\admin\validate\Teacher');
            if (true !== $result)
                return $this->error($result, 'add');
            if ($filename) {
                $param['head_url'] = $filename;
            }

            $param['class_ids'] = implode(',', $param['class_ids']);
            $res = Teacher::update($param, ['id'=>$id]);
            if ($res) {
                return $this->success('修改成功', 'index');
            } else {
                return $this->error('修改失败', 'add');
            }
        } else {
            $cls = Cls::all();
            $teacher = Teacher::get($id);
            $teacher['class_ids'] = explode(',', $teacher['class_ids']);
            $this->assign('cls', $cls);

            $this->assign('teacher', $teacher);
            return view('edit');
        }
    }

    public function del($id) {
        $teacher = Teacher::get($id);
        $teacher->is_del = 2;
        $res = $teacher->isUpdate(true)->save();
        if ($res) {
            return $this->success('删除成功', 'index');
        } else {
            return $this->error('删除失败', 'index');
        }
    }

    public function upload($file){
        $config = Config::get('aliyun_oss');

        $video=$file->getRealPath();
        $fileName = $file->getInfo()['name'];
        $fileType = substr($fileName,strripos($fileName,".")+1);
        $key = time() . rand(10000, 99999999).".".$fileType;
        $res = Oss::publicUpload($config['Bucket'], $key,$video);
        if ($res){
            $url = 'https://'.$config['Bucket'].'.'.$config['Endpoint'].'/'.$key;
        }
        return $url;
    }
}